package com.example.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.generator.domain.TBill;
import com.example.generator.service.TBillService;
import com.example.util.CommonUtil;
import com.example.vo.BillVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author ：chengyoubang
 * @version ：V1.0
 * @program ：hmall
 * @date ：Created in 2024/11/10 1:25
 * @description ：
 */
@RestController
public class TBillController {

    @Resource
    TBillService billService;

    @GetMapping("getBill")
    public String getBill(@RequestParam String startDate,@RequestParam String endDate) {
        List<TBill> bills = billService.list(new LambdaQueryWrapper<TBill>()
                .between(TBill::getBillDate,startDate,endDate)
                .orderByAsc(TBill::getBillDate).orderByAsc(TBill::getAddTime));
        Map<Integer, List<TBill>> billsMap = bills.stream().collect(Collectors.groupingBy(TBill::getBillType));
        List<TBill> incomeBills = billsMap.get(1);
        List<TBill> outcomeBills = billsMap.get(2);
        int maxSize = Math.max(CommonUtil.nvlSize(incomeBills),CommonUtil.nvlSize(outcomeBills));
        incomeBills= initBills(incomeBills, maxSize, 1);
        outcomeBills = initBills(outcomeBills, maxSize, 2);
        ArrayList<BillVo> billVos = new ArrayList<>();
        for (int i = 0; i < incomeBills.size(); i++) {
            TBill incomeBill = incomeBills.get(i);
            TBill outcomeBill = outcomeBills.get(i);
            BillVo billVo = new BillVo();
            billVo.setIncomeData(incomeBill);
            billVo.setOutcomeData(outcomeBill);
            billVos.add(billVo);
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("bills",billVos);
        return jsonObject.toString();
    }
    @GetMapping("_addBill")
    public String _add(Integer billId){
        TBill byId;
        if(billId==-1){
            byId = new TBill();
            byId.setAddTime(new Date());
            byId.setUpdateTime(new Date());
            billService.save(byId);
        }else{
            byId = billService.getById(billId);
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("bill",byId);
        return jsonObject.toString();
    }
    @PostMapping("editBill")
    public Boolean editBill(@RequestBody TBill tBill){
        TBill oldBill = billService.getById(billService.getById(tBill.getId()));
        tBill.setAddTime(oldBill.getAddTime());
        tBill.setUpdateTime(new Date());
        billService.updateById(tBill);
        return true;
    }


    public List<TBill> initBills(List<TBill> bills,int size,int type){
        bills= bills==null?new ArrayList<>():bills;
        if(size>bills.size()){
            for (int i = bills.size(); i <size ; i++) {
                bills.add(i,new TBill());
            }
        }
        final BigDecimal[] totalAmount = {new BigDecimal(0)};
        bills.forEach(tBill -> totalAmount[0] = totalAmount[0].add(tBill.getBillAmount()));
        TBill tBill = new TBill();
        tBill.setBillAmount(totalAmount[0]);
        tBill.setBillDesc(type==1?"收入合计":"支出合计");
        bills.add(tBill);
        return bills;
    }
}

